<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Common\AdminController;
class PicturesController extends AdminController {
    public function showlist(){
        //相册展示
        $pics_info = D('Pics')->select();
        $this->assign('pics_info',$pics_info);
        $this->display();
    }
    function add(){
        if(IS_POST){
            $data=I('post.');
            $data['add_time']=$data['upd_time']=time();
            $data['cate_id']=12;
            if($new_id=D('Pics')->add($data)){
                $this->del_pics($new_id);
                $this->success('相册添加成功',U('Pictures/showlist'),2);
            }else{
                $this->error('相册添加失败',U('Pictures/add',array('Pics_id'=>$new_id)),3);
            }
        }
        $this->display();
    }
    //删除相册
    function del(){
        //接受传递的参数
        $pics_id = I('post.pics_id');
//        echo json_encode($pics_id);
        $picture_info = D('Picture')->where(array('pics_id'=>$pics_id))->select();
        if(!empty($picture_info)){
            foreach($picture_info as $k=>$v){
                if(file_exists($v['picture_small'])){unlink($v['picture_small']);}
                if(file_exists($v['picture_big'])){unlink($v['picture_big']);}
            }
            $pics_info = D('Pics')->where(array('pics_id'=>$pics_id))->find();
            if(file_exists($pics_info['pics_logo'])){unlink($pics_info['pics_logo']);}
            if(file_exists($pics_info['pictures'])){unlink($pics_info['pictures']);}
            if(D('Pics')->delete($pics_id) && D('Picture')->where(array('pics_id'=>$pics_id))->delete()){
                echo json_encode(array('status'=>1));
            }else{
                echo json_encode(array('status'=>0));
            }
        }else{
            $pics_info = D('Pics')->where(array('pics_id'=>$pics_id))->find();
            if(file_exists($pics_info['pics_logo'])){unlink($pics_info['pics_logo']);}
            if(file_exists($pics_info['pictures'])){unlink($pics_info['pictures']);}
            if(D('Pics')->delete($pics_id)){
                echo json_encode(array('status'=>1));
            }else{
                echo json_encode(array('status'=>0));
            }
        }

    }
    function del_pics($id){
    $files = $_FILES['file'];
    foreach($files['error'] as $v) {
        if ($v == 0) {
            //设置保存的根路径
            $cfg = array(
                'rootPath' => './Public/Uploads/pics/', //保存根路径
            );
            //实例化Uploads类
            $upd = new \Think\Upload($cfg);
            //调用批量上传的方法
            $z = $upd->upload(array($files));
//                dump($z);
            //给图片制作缩略图,遍历已经上传好的图片
            $im = new \Think\Image();
            foreach($z as $k=>$vv){
                $old_path = $upd->rootPath.$vv['savepath'].$vv['savename'];
                //制作logo
                $im->open($old_path);
                $im->thumb(110,73,6);
                $pics_logo = $upd->rootPath.$vv['savepath'].'small_'.$vv['savename'];
                $im->save($pics_logo);
                $im->thumb(580,387,6);
                $pictures = $upd->rootPath.$vv['savepath'].'big_'.$vv['savename'];
                $im->save($pictures);
                //删除原来的旧图
                unlink($old_path);
                //将缩略图存储给数据库
                $data = array(
                    'pics_id'=>$id,
                    'pics_logo'=>$pics_logo,
                    'pictures'=>$pictures
                );
                D('Pics')->save($data);
            }

        }
    }
}
}

